<?php

namespace App\Task\Stat;

use App\Model\Stat\StatRegLog;
use App\Model\UserProfile;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 注册统计
 * @Crontab(name="StatRegTask", rule="*\/30 * * * *", callback="execute", memo="每日注册统计", singleton=false)
 */
class StatRegTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $model = new StatRegLog();
        $where = [
            ['created_at', '>=', date('Y-m-d H:i:s', time() - 1800)]
        ];
        $model->stat_ts = time();
        $model->reg_num = UserProfile::where($where)->count();
        $where1 = array_merge([['gender', 1]], $where);
        $model->reg_male_num = UserProfile::where($where1)->count();
        $where2 = array_merge([['gender', 0]], $where);
        $model->reg_female_num = UserProfile::where($where2)->count();
        $model->saveOrFail();
    }
}
